package com.cx.controller.admin.basic;

import com.cx.service.RedisService;
import com.cx.service.admin.basic.ChainBasicUserManager;
import com.cx.service.admin.basic.UploadManager;
import com.cx.utils.RedisKeyUtil;
import org.cx.base.ApiModel;
import org.cx.base.BaseController;
import org.cx.constant.BasicConstants;
import org.cx.constant.ServerCodeType;
import org.cx.entity.Page;
import org.cx.entity.PageData;
import org.cx.util.AssertUtil;
import org.cx.util.DesUtils;
import org.cx.util.ExcelUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@RestController("admin/basic/user")
@RequestMapping("admin/basic/user")
public class UserController extends BaseController {

    @Resource
    private ChainBasicUserManager chainBasicUserService;

    @Resource
    private RedisService jedisService;

    @Resource
    private UploadManager uploadService;

    @ResponseBody
    @RequestMapping(value = "/findUserDetailPage")
    public ApiModel findUserDetailPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Page pg =this.getPage();
        PageData pd = (PageData) pg.getPd();
        pd.put("orgId",getUserByToken().get("orgId"));
        List<PageData> list = chainBasicUserService.findUserDetailPage(pg);
        pg.setPd(list);
        return buildSuccMsg(pg);
    }

    @ResponseBody
    @RequestMapping(value = "/findClerkDetailPage")
    public ApiModel findClerkDetailPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Page pg =this.getPage();
        PageData pd = (PageData) pg.getPd();
        if(pd.get("orgId")== null || pd.get("orgId").equals("")) {
            pd.put("orgId", getUserByToken().get("orgId"));
        }
        pd.put("pid", getUserByToken().get("orgId"));
        List<PageData> list = chainBasicUserService.findClerkDetailPage(pg);
        pg.setPd(list);
        return buildSuccMsg(pg);
    }

    @ResponseBody
    @RequestMapping(value = "/exportUserDetail")
    public void exportUserDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Page pg =this.getPage();
        PageData pd = (PageData) pg.getPd();
        AssertUtil.isNotNull(getUserByToken(request.getParameter("token")), ServerCodeType.USER_NOT_LOGIN.getMessage());
        pd.put("orgId",getUserByToken(request.getParameter("token")).get("orgId"));
     //      pd.put("orgId",1);
        pg.setPd(pd);
        pg.setShowCount(BasicConstants.ExportData.maxNum);
        List<PageData> list = chainBasicUserService.findUserDetailPage(pg);
        String[] title ={"序号","员工姓名","员工手机号码","创建时间","角色","状态","归属门店"};
        String[] onlyFilds = {"id","userName","userPhone","createTime","roleName","stateCh","orgName"};

        ExcelUtils.fillExcelForFile(list,title,onlyFilds,"userDetail_",response);
    }

    @ResponseBody
    @RequestMapping(value = "/exportClerkDetail")
    public void exportClerkDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Page pg =this.getPage();
        PageData pd = (PageData) pg.getPd();
        AssertUtil.isNotNull(getUserByToken(request.getParameter("token")), ServerCodeType.USER_NOT_LOGIN.getMessage());
        if(pd.get("orgId")== null || pd.get("orgId").equals("")) {
            pd.put("orgId", getUserByToken(request.getParameter("token")).get("orgId"));
        }
        pd.put("pid", getUserByToken(request.getParameter("token")).get("orgId"));
        //      pd.put("orgId",1);
        pg.setPd(pd);
        pg.setShowCount(BasicConstants.ExportData.maxNum);
        List<PageData> list = chainBasicUserService.findClerkDetailPage(pg);
        String[] title ={"门店编号","门店名称","店员姓名","店员手机","创建时间","角色"};
        String[] onlyFilds = {"orgId","orgName","userName","userPhone","createTime","roleName"};

        ExcelUtils.fillExcelForFile(list,title,onlyFilds,"clerkDetail_",response);
    }

    @ResponseBody
    @RequestMapping(value = "/updateUser")
    public ApiModel updateUser() throws Exception {
        PageData pd =this.getPageData();
        //根据手机号码查询用户是否存在
        PageData userpd =  chainBasicUserService.findUserInfoByPhone(pd);
        if(userpd == null){
            return buildErrorMsg(userpd,DATA_ERROR,"用户不存在");
        }
        Integer result = chainBasicUserService.updateUser(pd);
        //   pg.setPd(list);
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/insertUser")
    public ApiModel insertUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        PageData pd =this.getPageData();
        //根据手机号码查询用户是否存在

        PageData userpd =  chainBasicUserService.findUserInfoByPhone(pd);
        if(userpd != null){
            return buildErrorMsg(userpd,DATA_ERROR,"用户已存在");
        }
        PageData user = getUserByToken(request.getHeader("token"));
        pd.put("orgId",user.get("orgId"));
        String pwd = DesUtils.getMD5String(pd.getString("userPwd"));
        pd.put("userPwd",pwd);
        pd.put("topOrgId",user.get("topOrgId"));
        pd.put("createBy",user.get("userPhone"));
        Integer result  = chainBasicUserService.insertUser(pd);
        if(result<=0){
            return buildErrorMsg(pd,DATA_ERROR,FAIL);
        }
        return buildSuccMsg(pd);
    }

    @ResponseBody
    @RequestMapping(value = "/saveExcelUser")
    public ApiModel saveExcelUser() throws Exception {
  //      List<PageData> pdlist = uploadController.readExcelContent("file:///E:\\projects\\chainJKT\\chainJKT\\target\\data\\upload_files\\bbb.xlsx");
//        Integer result  = uploadService.saveExcelObjectIntoTable("file:///E:\\\\projects\\\\chainJKT\\\\chainJKT\\\\target\\\\data\\\\upload_files\\\\bbb.xlsx");
//        if(result<=0){
//            return buildErrorMsg(null,DATA_ERROR,FAIL);
//        }
        return buildSuccMsg(null);
    }



}

